<?php
namespace Common\Model;

/**
 * 子管理员房间权限记录表
 * @modify trainyao 2016-06-28 创建
 * @index hotelRoomAuthority(`isdelete`, `adminlogin_id`, `hotelentity_id`)
 */

Class HotelRoomAuthorityModel extends \Common\Model\BaseModel
{
    protected $trueTableName = 'tb_hotel_room_authority';
    protected $field = 'id,hotelentity_id,adminlogin_id,roominfo_id,isdelete';
    

    public function getInfoByAdminHotel($adminId, $hotelId) {
        $option = array(
            'isdelete' => NOT_DELETE,
            'adminlogin_id' => $adminId,
        );
        if (is_array($hotelId)) {
            $hotelId = implode(',', $hotelId);
        }
        if (strpos($hotelId, ',') === false) {
            $option['hotelentity_id'] = $hotelId;
        } else {
            $option['hotelentity_id'] = array('in', $hotelId);
        }

        $data = $this->where($option)->field($field)->select();
        return empty($data) ? array() : $data;
    }

    /**
     * 增加房间权限
     * @modify trainyao 2016-06-28          创建
     * @param  integer   $accountId          子管理员ID
     * @param  array   $roomAuthorityToAdd   权限信息数组 [{hotelentity_id, roominfo_id}, {}, {}...]
     * @return boolean 执行结果
     */
    public function addInfo($accountId, $roomAuthorityToAdd) {
        $creator = getUserId();
        foreach ($roomAuthorityToAdd as &$roomAuthorityItem) {
            $roomAuthorityItem['creator'] = $creator;
            $roomAuthorityItem['operator'] = $creator;
            $roomAuthorityItem['adminlogin_id'] = $accountId;
        }
        unset($roomAuthorityItem);

        return $this->addAll($roomAuthorityToAdd) ? true : false;
    }

    /**
     * 根据房间ID删除房间权限
     * @modify trainyao 2016-06-28          创建
     * @param  integer   $accountId          子管理员ID
     * @param  array   $roomId   房间ID数组
     * @return boolean 执行结果
     */
    public function deleteInfoByRoom($accountId, $roomId) {
        $option = array(
            'isdelete' => NOT_DELETE,
            'adminlogin_id' => $accountId,
        );
        if (is_array($roomId)) {
            $roomId = implode(',', $roomId);
        }
        if (strpos($roomId, ',') === false) {
            $option['roominfo_id'] = $roomId;
        } else {
            $option['roominfo_id'] = array('in', $roomId);
        }

        return $this->where($option)->save(array(
            'isdelete' => IS_DELETE
        )) === false ? false : true;
    }

    /**
     * 根据客栈ID删除房间权限
     * @modify trainyao 2016-06-28          创建
     * @param  integer   $accountId          子管理员ID
     * @param  array   $hotelId   客栈ID数组
     * @return boolean 执行结果
     */
    public function deleteInfoByHotel($accountId, $hotelId) {
        $option = array(
            'isdelete' => NOT_DELETE,
            'adminlogin_id' => $accountId,
        );
        if (is_array($hotelId)) {
            $hotelId = implode(',', $hotelId);
        }
        if (strpos($hotelId, ',') === false) {
            $option['hotelentity_id'] = $hotelId;
        } else {
            $option['hotelentity_id'] = array('in', $hotelId);
        }

        return $this->where($option)->save(array(
            'isdelete' => IS_DELETE
        )) === false ? false : true;
    }

    /**
     * 根据客栈ID 房间ID 获取数据
     * @modify wenhui 2017-03-16          创建
     * @param  integer   $hotelId          客栈ID
     * @param  array   $roomId   房间ID
     * @return $data 执行结果
     */
    public function getInfoByHotelRoom($hotelId, $roomId) {
        $option = array(
            'isdelete' => NOT_DELETE,
            'hotelentity_id' => $hotelId,
        );
        if (is_array($roomId)) {
            $roomId = implode(',', $roomId);
        }
        if (strpos($roomId, ',') === false) {
            $option['roominfo_id'] = $roomId;
        } else {
            $option['roominfo_id'] = array('in', $roomId);
        }

        $data = $this->where($option)->field($field)->select();
        return empty($data) ? array() : $data;
    }

    /**
     * 根据管理员ID 房间ID 获取数据
     * @modify wenhui 2017-03-16          创建
     * @param  integer   $userLoginId          管理员ID
     * @param  array   $roomId   房间ID
     * @param  array   $hotelIds   客栈ID
     * @return $data 执行结果
     */
    public function getInfoByUserRoom($userLoginId, $roomId, $hotelIds = null) {
        $option = array(
            'isdelete' => NOT_DELETE,
            'adminlogin_id' => $userLoginId,
        );
        if (is_array($roomId)) {
            $roomId = implode(',', $roomId);
        }
        if (strpos($roomId, ',') === false) {
            $option['roominfo_id'] = $roomId;
        } else {
            $option['roominfo_id'] = array('in', $roomId);
        }

        if (!is_null($hotelIds)) {
            if (is_array($hotelIds)) {
                $hotelIds = implode(',', $hotelIds);
            }
            if (strpos($hotelIds, ',') === false) {
                $option['hotelentity_id'] = $hotelIds;
            } else {
                $option['hotelentity_id'] = array('in', $hotelIds);
            }
        }

        $data = $this->where($option)->field($field)->select();
        return empty($data) ? array() : $data;
    }
}
